package com.ai.chatai.repository;

import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository// 表示这个类是一个存储库，用于数据访问
public class InMemoryChatHistoryRepository implements ChatHistoryRepository {
    private final Map<String, List<String>> chatHistory = new HashMap<>();// 存储聊天记录的Map

    @Override
    public void save(String type, String chatId) {
        /*if (!chatHistory.containsKey(type))
         {
             chatHistory.put(type, new ArrayList<>());
         }
         List<String> chatIds = chatHistory.get(type);
         以上代码可以简化为下面一行代码
         */
        List<String> chatIds = chatHistory.computeIfAbsent(type, k -> new ArrayList<>());
        if (chatIds.contains(chatId)) {
            return;
        }
        chatIds.add(chatId);
    }

    @Override
    public void delete(String type, String chatId) {
        return;
    }

    @Override
    public List<String> getChatIds(String type) {
         /*if (!chatHistory.containsKey(type))
         {
             return new ArrayList<>();
         }
         return chatHistory.get(type);
         简化为以下一行代码
         */
        return chatHistory.getOrDefault(type, new ArrayList<>());
    }
}
